% Classical Minimum Distance Estimator Basic Model 
% Fits declining boost in visits
% We use moments coming from median split regression

clear
Model = 2 % Stackelberg
Version = 10 % 8 moments

% Moments:  % HH joint visits +  number of visits x month 
RealMoments =  [0.339; 1.004; 1.697 	 	  ]
W = [ 0.103 ; 0.101 ; 0.090 ]
MomentCovar     = diag(W);

%Starting parameters
AllStart = [0.12; 2 ; 0.3 ; 1.3 ; 1.7]

n0 = 1;
nz = 5;


for n = n0:nz
  
Repetition      = n

format shortg;
T               = clock ;
Hour            = T(4);
Min             = T(5);
TimeStamp       = [Hour, Min]

StartPar =     AllStart(n:n,:);



lb = [0];
ub = [10000];

nonlcon = @Constraint;
options     = optimset('display', 'iter', 'TolFun', 1e-18, 'MaxIter',20000, 'MaxFunEvals',20000);
[BetaHat,fval,exitflag,output]     = fmincon(@(param) v10_LossFunction(param, RealMoments, MomentCovar), [StartPar],  [],[],[],[],lb,ub,[],options);

BetaHat;


% Simulated moments
SimMomentst = v10_Moments([BetaHat]);
SimMoments = [SimMomentst, RealMoments]


% Store Data
filename1 = sprintf('%s_%d.csv','results/v10_Parameters',n)
W2 = W';
optvar = options.TolFun;
itervar = output.iterations;
BetaHat = BetaHat';
StartPar = StartPar';
DataStore = padcat(BetaHat, SimMomentst, RealMoments,   W, StartPar, fval, exitflag, Model,  optvar, itervar); 
csvwrite(filename1,DataStore);

end
